# _*_coding:utf-8_*_
# @auth: SKYNE
import urllib.request, time, json, requests, threading
from lxml import html
from twilio.rest import Client


def get_weather():
    actual_url = "http://api.yytianqi.com/observe?city=CH180201&key=k282cdhteicpokgv"
    maxin_url = "http://anyang.tianqi.com/wenfengqu/"
    weather = urllib.request.urlopen(actual_url)
    data = weather.read().decode('utf-8')
    weather = json.loads(data)['data']
    html1 = requests.get(url=maxin_url).content
    selecor = html.fromstring(html1)
    max_temp = selecor.xpath('//*[@id="t_temp"]/font[1]/text()')[0]
    min_temp = selecor.xpath('//*[@id="t_temp"]/font[2]/text()')[0]
    weather['max_temp'] = max_temp
    weather['min_temp'] = min_temp
    return weather


def combine_info(weather):
    info = '天气状况：' + weather['tq'] + '\n' + '实时温度：' + weather['qw'] \
           + '℃\n' + '今日最低：%s\t最高：%s\n' % (weather['min_temp'], weather['max_temp']) \
           + '当前风速风向：' + weather['fl'] + '的' + weather['fx'] \
           + '\n' + '当前湿度：' + weather['sd'] + '\n' + '更新时间：' \
           + weather['lastUpdate'] + '\nFrom----SKYNE'
    return info


def Send_SMS(info):
    account_sid = "AC9e094a6ed2268facade109a556f9f752"
    auth_token = "c51ff7f0460b07c4b26019c252535634"
    client = Client(account_sid, auth_token)
    client.messages.create(
        to="+8618211635532",
        from_="+1323-612-0204",
        body=info
    )
    client.messages.create(
        to="+8615737209526",
        from_="+1323-612-0204",
        body=info
    )
    print(info)


def judge_info():
    weather = get_weather()
    print("正在执行Judge_info\t%s" % weather['lastUpdate'])
    while True:
        timenow = time.localtime()
        if timenow[3] == 22:
            time.sleep(25200)
            weather = get_weather()
            if int(weather['qw']) >= 30:
                print("当前气温大于30度，正在发送短信\t%s" % weather['lastUpdate'])
                info = update_info()
                info = '\n糟糕，高温预警\n' + info
                Send_SMS(info)
                print("已经成功发送短信\t%s" % weather['lastUpdate'])
            if str(weather['numtq']) not in ['00', '01']:
                print("当前天气出现变化，正在发送短信\t%s" % weather['lastUpdate'])
                info = update_info()
                info = '\n糟糕，要变天啦。\n' + info
                Send_SMS(info)
                print("已经成功发送短信\t%s" % weather['lastUpdate'])
            time.sleep(3000)


def judge_time():
    weather = get_weather()
    print("正在执行Judge_time\t%s" % weather['lastUpdate'])
    while True:
        time.sleep(60)
        timenow = time.localtime()
        if timenow[3] == 7:
            print("正在发送早间天气状况\t%s" % weather['lastUpdate'])
            info = update_info()
            info = '\n早安，思密达。\n今天天气如下：\n' + info
            Send_SMS(info)
            print("已经成功发送短信\t%s" % weather['lastUpdate'])
            time.sleep(86000)


def update_info():
    weather = get_weather()
    info = combine_info(weather)
    return info


def main():
    print("程序正在运行中。。。。。")
    j_time = threading.Thread(target=judge_time)
    j_info = threading.Thread(target=judge_info)
    j_time.start()
    j_info.start()


if __name__ == '__main__':
    main()
